//package com.bwda.dsrs.filecenter.service.impl;
//
//
//import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
//import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
//import com.bwda.dsrs.filecenter.api.OrgServiceApi;
//import com.bwda.dsrs.filecenter.api.clientappversion.datadict.DatadictQueryForm;
//import com.bwda.dsrs.filecenter.api.clientappversion.datadict.DatadictVo;
//import com.bwda.dsrs.filecenter.domain.BusinessConstants;
//import com.bwda.dsrs.filecenter.domain.api.DatadictUpdateForm;
//import com.bwda.dsrs.filecenter.domain.condition.sensitivelevel.SensitiveLevelCondition;
//import com.bwda.dsrs.filecenter.domain.condition.strategycontrol.StrategyControlCondition;
//import com.bwda.dsrs.filecenter.domain.condition.userstrategyrel.UserStrategyRelCondition;
//import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
//import com.bwda.dsrs.filecenter.domain.form.strategycontrol.StrategyBuildInForm;
//import com.bwda.dsrs.filecenter.domain.form.strategycontrol.StrategyControlQueryForm;
//import com.bwda.dsrs.filecenter.domain.po.sensitivelevel.SensitiveLevelPo;
//import com.bwda.dsrs.filecenter.domain.po.strategycontrol.StrategyControlPo;
//import com.bwda.dsrs.filecenter.domain.po.userstrategyrel.UserStrategyRelPo;
//import com.bwda.dsrs.filecenter.domain.vo.strategycontrol.StrategyControlVo;
//import com.bwda.dsrs.filecenter.mapper.dsrs.StrategyControlMapper;
//import com.bwda.dsrs.filecenter.service.SensitiveLevelService;
//import com.bwda.dsrs.filecenter.service.StrategyControlService;
//import com.bwda.dsrs.filecenter.service.UserStrategyRelService;
//import com.bwda.dsrs.filecenter.util.RiskModelUtil;
//import com.bwda.dsrs.systemservice.domain.po.userlevel.UserLevelPo;
//import com.bwda.sdk.security.exception.BwdaSecurityException;
//import org.apache.commons.lang.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.util.*;
//
///**
// * 策略管理--管控策略--服务层实现类
// *
// * @author zhangyinmei
// * @date 2019年4月28日
// */
//@Service
//public class StrategyControlServiceImpl extends BaseServiceImpl<StrategyControlPo, StrategyControlCondition, StrategyControlMapper> implements StrategyControlService {
//
//    private static final Logger logger = LoggerFactory.getLogger(StrategyControlServiceImpl.class);
//
//    @Autowired
//    private OrgServiceApi orgServiceApi;
//
//    @Autowired
//    private UserStrategyRelService userStrategyRelService;
//
//    @Autowired
//    private SensitiveLevelService sensitiveLevelService;
//    /**
//     * 风险等级
//     */
//    private static final String RISK_LEVEL = "riskLevel";
//    /**
//     * 业务处理
//     */
//    private static final String BUSINESS_PROCESS = "businessProcess";
//
//
//    @Override
//    public void updateBuildInStrategy(StrategyBuildInForm form) throws BusinessServiceException {
//        if (form.getRiskModel() == null) {
//            throw new BusinessServiceException("风险模式未设置");
//        }
//
//        int riskModel = form.getRiskModel();
//        if (BusinessConstants.RiskModel.USER == riskModel) {
//            List<UserLevelPo> userLevelPoListlist = orgServiceApi.queryUserLevelList();
//            //TODO 如果没有用户职级，提示设置用户职级，不要抛用户不友好的异常，TODO 不一定是加在这里
//            if (userLevelPoListlist == null || userLevelPoListlist.size() == 0) {
//                throw new BusinessServiceException("请设置用户分级");
//            }
//        }
//        String rickModelCode = form.getRiskModelCode();
//        if (StringUtils.isNotEmpty(rickModelCode)) {
//            //TODO  看来是特地删掉的，先放出来
//            orgServiceApi.updateMutexStatusByCode(rickModelCode);
//        }
//        //查询是否存在已使用的内置策略
//        List<StrategyControlPo> usingList = this.queryUsingModelStrategy();
//        //存在则初始化内置策略信息
//        if (usingList != null && usingList.size() > 0) {
//            for (StrategyControlPo strategyControlPo : usingList) {
//                strategyControlPo.setSensitiveLevel(0);
//                strategyControlPo.setBusinessIds(BusinessConstants.BLANK_STR);
//                strategyControlPo.setDelFlag(BusinessConstants.DelFlag.DELETED);
//                strategyControlPo.setRiskValue(BusinessConstants.BLANK_STR);
//                this.getMapper().update(strategyControlPo);
//            }
//        }
//        //不存在则配置内置策略信息
//        if (BusinessConstants.RiskModel.COMPOSE == riskModel) {
//            if (form.getPackagesNum() == null) {
//                throw new BwdaSecurityException("组合模式未设置风险等级数量");
//            }
//            //用户等级
//            int userLevel = 0;
//            //文件等级
//            int fileLevel = 0;
//            //查询用户等级
//            List<UserLevelPo> userlist = orgServiceApi.queryUserLevelList();
//            userLevel = userlist.size();
//
//            //查询文件等级
//            SensitiveLevelCondition sensitiveLevelCondition = new SensitiveLevelCondition();
//            sensitiveLevelCondition.setSensitiveState(BusinessConstants.NumberConstant.ZERO);
//            sensitiveLevelCondition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
//            List<SensitiveLevelPo> fileList = sensitiveLevelService.queryList(sensitiveLevelCondition);
//            fileLevel = fileList.size();
//            //获取风险值
//            Integer[] rvArr = RiskModelUtil.getRiskValues(userLevel, fileLevel);
//            //获取每个策略包对应相关的风险等级和风险值
//            Map<Integer, String> map = RiskModelUtil.getPackagesData(rvArr, form.getPackagesNum());
//            Set<Integer> keySet = map.keySet();
//            //获取内置组合策略记录数
//            int pageSize = keySet.size();
//            //获取内置组合策略记录
//            List<StrategyControlPo> poList = this.queryStrategyByMolde(riskModel, pageSize);
//            if (poList == null || poList.size() == 0) {
//                throw new BwdaSecurityException("不存在内置组合策略");
//            }
//            //循环设置组合策略
//            for (Integer integer : keySet) {
//                StrategyControlPo strategyControlPo = poList.get(integer - 1);
//                strategyControlPo.setSensitiveLevel(integer);
//                strategyControlPo.setRiskValue(map.get(integer));
//                strategyControlPo.setDelFlag(BusinessConstants.DelFlag.NOT_DELETED);
//                strategyControlPo.setUpdateTime(new Date());
//                this.update(strategyControlPo);
//            }
//            DatadictUpdateForm dictForm = new DatadictUpdateForm();
//            dictForm.setDataDictCode(BusinessConstants.RISK_LEVEL_CODE);
//            dictForm.setDataDictValue(form.getPackagesNum().toString());
//            orgServiceApi.updateDatadict(dictForm);
//        } else {
//            int pageSize = 0;
//            List<UserLevelPo> userlist = null;
//            List<SensitiveLevelPo> fileList = null;
//            //判断模式，设置字典类型
//            if (BusinessConstants.RiskModel.USER == riskModel) {
//                userlist = orgServiceApi.queryUserLevelList();
//                //TODO 如果没有用户职级，提示设置用户职级，不要抛用户不友好的异常，TODO 不一定是加在这里
//                pageSize = userlist.size();
//            } else if (BusinessConstants.RiskModel.FILE == riskModel) {
//                SensitiveLevelCondition sensitiveLevelCondition = new SensitiveLevelCondition();
//                sensitiveLevelCondition.setSensitiveState(BusinessConstants.NumberConstant.ZERO);
//                sensitiveLevelCondition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
//                fileList = sensitiveLevelService.queryList(sensitiveLevelCondition);
//                pageSize = fileList.size();
//            }
//            List<StrategyControlPo> poList = this.queryStrategyByMolde(riskModel, pageSize);
//            if (poList == null || poList.size() == 0) {
//                throw new BwdaSecurityException("不存在用户或文件策略");
//            }
//            for (int e = 0; e < poList.size(); e++) {
//                StrategyControlPo strategyControlPo = poList.get(e);
//                if (BusinessConstants.RiskModel.USER == riskModel) {
//
//                    strategyControlPo.setSensitiveLevel(userlist.get(e).getLevelScore());
//                    strategyControlPo.setRiskValue(String.valueOf(userlist.get(e).getLevelScore()));
//                } else if (BusinessConstants.RiskModel.FILE == riskModel) {
//                    strategyControlPo.setSensitiveLevel(fileList.get(e).getSensitiveLevel());
//                    strategyControlPo.setRiskValue(String.valueOf(fileList.get(e).getSensitiveLevel()));
//                }
//                strategyControlPo.setDelFlag(BusinessConstants.DelFlag.NOT_DELETED);
//                strategyControlPo.setUpdateTime(new Date());
//                this.update(strategyControlPo);
//            }
//            DatadictUpdateForm dictForm = new DatadictUpdateForm();
//            dictForm.setDataDictCode(BusinessConstants.RISK_LEVEL_CODE);
//            dictForm.setDataDictValue(BusinessConstants.StringConstant.ZERO);
//            orgServiceApi.updateDatadict(dictForm);
//        }
//    }
//
//    @Override
//    public List<String> queryStrategyByRisk(StrategyControlQueryForm form) throws BusinessServiceException {
//        List<String> businessId = new ArrayList<String>();
//        List<StrategyControlPo> strategyList = null;
//        //敏感等级
//        int sensitiveLevel = 0;
//        if (StringUtils.isEmpty(form.getUserCode())) {
//            throw new BwdaSecurityException("用户code为空");
//        }
//        //自定义策略优先查看
//        UserStrategyRelCondition rel = new UserStrategyRelCondition();
//        rel.setUserCode(form.getUserCode());
//        List<UserStrategyRelPo> rels = userStrategyRelService.queryList(rel);
//        if (rels != null && !rels.isEmpty()) {
//            StrategyControlPo strategyControlPo = this.query(rels.get(0).getStrategyId());
//            businessId.add(strategyControlPo.getBusinessIds());
//            return businessId;
//        }
//        //获取系统开启模式
//        int riskModel = 0;
//        ResponseEntity<CentreListResponse<DatadictVo>> responseEntity = this.queryRiskModelActive();
//        List<DatadictVo> lists = responseEntity.getData().getDataList();
//        if (lists == null || lists.size() == 0) {
//            throw new BusinessServiceException("内置策略未设置管控模式。");
//        }
//        riskModel = Integer.parseInt(lists.get(0).getDataDictValue());
//
//        if (riskModel == 0) {
//            throw new BusinessServiceException("未启用管控模式");
//        }
//        StrategyControlCondition condition = new StrategyControlCondition();
//        condition.setRiskModel(riskModel);
//        condition.setDelFlag(BusinessConstants.DelFlag.NOT_DELETED);
//        //组合模式
//        String rickValue = "";
//        if (BusinessConstants.RiskModel.COMPOSE == riskModel) {
//            Integer userLevel = form.getUserLevel();
//            Integer fileLevel = form.getFileLevel();
//            if (userLevel == null || fileLevel == null) {
//                condition.setRiskValue("");
//            } else {
//                rickValue = String.valueOf(userLevel * fileLevel);
//                condition.setRiskValue("");
//            }
//        } else {
//            //判断模式，设置字典类型
//            if (BusinessConstants.RiskModel.USER == riskModel) {
//                sensitiveLevel = form.getUserLevel();
//            } else if (BusinessConstants.RiskModel.FILE == riskModel) {
//                sensitiveLevel = form.getFileLevel();
//            }
//            condition.setSensitiveLevel(sensitiveLevel);
//        }
//        //查询内置策略
//        strategyList = this.queryStrategyByRiskLevel(condition);
//        if (strategyList != null && !strategyList.isEmpty()) {
//            //组合模式存的是 1，2，22，44 特殊处理
//            if (BusinessConstants.RiskModel.COMPOSE == riskModel) {
//                for (StrategyControlPo strategyControlPo : strategyList) {
//                    String riskValue = strategyControlPo.getRiskValue();
//                    String[] values = riskValue.split(",");
//                    for (String v : values) {
//                        if (v.equals(rickValue)) {
//                            String businessIds = strategyControlPo.getBusinessIds();
//                            if (StringUtils.isNotEmpty(businessIds)) {
//                                businessId.add(businessIds);
//                            }
//
//                            break;
//                        }
//                    }
//
//
//                }
//
//            } else {
//                for (StrategyControlPo strategyControlPo : strategyList) {
//                    String businessIds = strategyControlPo.getBusinessIds();
//                    if (StringUtils.isNotEmpty(businessIds)) {
//                        businessId.add(businessIds);
//                    }
//                }
//            }
//
//
//        }
//        return businessId;
//    }
//
//    public ResponseEntity<CentreListResponse<DatadictVo>> queryRiskModelActive() throws BusinessServiceException {
//        DatadictQueryForm form = new DatadictQueryForm();
//        form.setDataDictTypeCode(BusinessConstants.RISK_MODEL_CODE);
//        form.setStatus(0);
//        return orgServiceApi.queryDatadictFullList(form);
//    }
//
//    /**
//     * 根据风险等级或者敏感值获取内置数据
//     *
//     * @param condition
//     * @return
//     */
//    public List<StrategyControlPo> queryStrategyByRiskLevel(StrategyControlCondition condition) {
//        return this.getMapper().queryStrategyByRiskLevel(condition);
//    }
//
//    /**
//     * 根据模式获取内置数据
//     * * @return
//     */
//    public List<StrategyControlPo> queryUsingModelStrategy() {
//        StrategyControlCondition condition = new StrategyControlCondition();
//        List<Integer> riskModelList = new ArrayList<Integer>();
//        riskModelList.add(BusinessConstants.RiskModel.USER);
//        riskModelList.add(BusinessConstants.RiskModel.FILE);
//        riskModelList.add(BusinessConstants.RiskModel.COMPOSE);
//        condition.setDelFlag(BusinessConstants.DelFlag.NOT_DELETED);
//        condition.setRiskModelList(riskModelList);
//        return this.queryList(condition);
//    }
//
//    /**
//     * 根据模式获取内置数据
//     *
//     * @param riskModel
//     * @return
//     */
//    public List<StrategyControlPo> queryStrategyByMolde(int riskModel, int pageSize) {
//        StrategyControlCondition condition = new StrategyControlCondition();
//        condition.setRiskModel(riskModel);
//        condition.setPageSize(pageSize);
//        condition.setPageNum(0);
//        condition.setOrderBy(BusinessConstants.Strategy.COLUMN_ID);
//        return this.queryList(condition);
//    }
//
////    @Override
////    public list<OrgTreeForm> queryOrgTree() throws SystemServiceException {
////        ResponseEntity<list<OrgTreeForm>> orgTreeFormListRE = systemServiceApi.queryTree();
////        return orgTreeFormListRE.getData();
////    }
//
//
//    @Override
//    public void toGetDataDictName(List<StrategyControlVo> voList) throws BusinessServiceException {
//        logger.info("获取数据字典名称。");
//        try {
//            //根据sensitiveLevel 和 businessIds
//            //遍历集合
//            for (StrategyControlVo strategyControlVo : voList) {
//                //获取风险等级
//                Integer sensitiveLevel = strategyControlVo.getSensitiveLevel();
//                //定义查询参数实体
//                DatadictQueryForm datadictQueryForm = new DatadictQueryForm();
//                //设置数据字典类别code为风险等级
//                datadictQueryForm.setDataDictTypeCode(RISK_LEVEL);
//                //调用字典表接口-查询风险等级
//                ResponseEntity<CentreListResponse<DatadictVo>> responseEntityToRiskLevel = orgServiceApi.queryDatadictFullList(datadictQueryForm);
//                List<DatadictVo> dataDictVoToRiskLevelList = responseEntityToRiskLevel.getData().getDataList();
//
//            }
//
//        } catch (Exception e) {
//            logger.info("字典工具类调用失败");
//            //throw new BusinessServiceException("字典工具类调用失败！");
//        }
//
//    }
//}